package com.ruoyi.binance.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binance.api.OrderApi;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.binance.domain.BinanceTransactionOrder;
import com.ruoyi.binance.service.IBinanceTransactionOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 交易订单Controller
 * 
 * @author zq
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/binance/order")
public class BinanceTransactionOrderController extends BaseController
{
    @Autowired
    private IBinanceTransactionOrderService binanceTransactionOrderService;

    /**
     * 查询交易订单列表
     */
    @PreAuthorize("@ss.hasPermi('binance:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(BinanceTransactionOrder binanceTransactionOrder)
    {
        startPage();
        List<BinanceTransactionOrder> list = binanceTransactionOrderService.selectBinanceTransactionOrderList(binanceTransactionOrder);
        return getDataTable(list);
    }

    /**
     * 导出交易订单列表
     */
    @PreAuthorize("@ss.hasPermi('binance:order:export')")
    @Log(title = "交易订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BinanceTransactionOrder binanceTransactionOrder)
    {
        List<BinanceTransactionOrder> list = binanceTransactionOrderService.selectBinanceTransactionOrderList(binanceTransactionOrder);
        ExcelUtil<BinanceTransactionOrder> util = new ExcelUtil<BinanceTransactionOrder>(BinanceTransactionOrder.class);
        util.exportExcel(response, list, "交易订单数据");
    }

    /**
     * 获取交易订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('binance:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(binanceTransactionOrderService.selectBinanceTransactionOrderById(id));
    }

    /**
     * 新增交易订单
     */
    @PreAuthorize("@ss.hasPermi('binance:order:add')")
    @Log(title = "交易订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BinanceTransactionOrder binanceTransactionOrder)
    {
        if(!binanceTransactionOrder.getCode().contains("USDT")){
            binanceTransactionOrder.setCode(binanceTransactionOrder.getCode() + "USDT");
        }
        binanceTransactionOrder.setOrgOrderId(IdUtil.getSnowflakeNextIdStr());
        binanceTransactionOrder.setType("MARKET");
        binanceTransactionOrder.setCreateBy(getUsername());
        binanceTransactionOrder.setCreateTime(new Date());
        int i = binanceTransactionOrderService.insertBinanceTransactionOrder(binanceTransactionOrder);
        if(i > 0){
            //调用下单接口
            JSONObject order = OrderApi.order(binanceTransactionOrder);
            if(StringUtils.isNotBlank(order.getString("code"))){
                binanceTransactionOrder.setStatus("REJECTED");
                binanceTransactionOrder.setRemark(order.getString("msg"));
            }else {
                binanceTransactionOrder.setStatus("PENDING_NEW");
                binanceTransactionOrder.setOrderId(order.getString("orderId"));
            }
            binanceTransactionOrderService.updateBinanceTransactionOrder(binanceTransactionOrder);
        }
        return toAjax(i);
    }

    /**
     * 修改交易订单
     */
    @PreAuthorize("@ss.hasPermi('binance:order:edit')")
    @Log(title = "交易订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BinanceTransactionOrder binanceTransactionOrder)
    {
        return toAjax(binanceTransactionOrderService.updateBinanceTransactionOrder(binanceTransactionOrder));
    }

    /**
     * 删除交易订单
     */
    @PreAuthorize("@ss.hasPermi('binance:order:remove')")
    @Log(title = "交易订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(binanceTransactionOrderService.deleteBinanceTransactionOrderByIds(ids));
    }
}
